function apiready() {
    const houseId = fxj.getSearch().get('houseId');
    const ja = new Vue({
        el: '#main',
        data: {
            rotate: 0, //图片旋转
            is_img_rotate: 'addimg_heng', //图片旋转
            add_img_button_div: true, //
            mask_img_rule: false, //img规则弹出层
            img_rotate: {
                transform: '',
            },
            house_img: { //房源图片数据
                imgbig: {
                    rotate: 0,
                    src: '',
                    order: '',
                    txt: '',
                    id: '',
                    name: '',
                },
                imgsmall: [],
            },
            img_all: 0,
            cover_src: '',
        },
        methods: {
            async delete_img() {
                const close = fxj.dialog.loading({noBg: true});
                const re = await ajax({
                    url: 'home/houseUpload/imgDelete',
                    data: {
                        houseId,
                        id: ja.house_img.imgbig.id,
                    },
                });
                close();
                if (re.err === 0) {
                    toast('删除成功');
                    for (let i = 0; i < ja.house_img.imgsmall.length; i++) {
                        if (ja.house_img.imgsmall[i].id === ja.house_img.imgbig.id) {
                            ja.house_img.imgsmall[i] = undefined;
                            ja.img_all -= 1;
                            break;
                        }
                    }
                    ja.house_img.imgsmall = ja.house_img.imgsmall.filter((el, i) => {
                        if (isExist(el)) {
                            el.order = ++i;
                            return true;
                        }
                        return false;
                    });
                }
                else {
                    toast(re.msg);
                }
            },
            addimg() {
                document.getElementById('addimgca').click();
            },
            async set_cover() {
                const close = fxj.dialog.loading({noBg: true});
                const re = await ajax({
                    url: 'home/houseManage/setCover',
                    data: {
                        houseId,
                        imgId: ja.house_img.imgbig.id,
                    },
                });
                close();
                toast(re.msg);
                if (re.err === 0) {
                    forEach(ja.house_img.imgsmall, img => {
                        if (img.id === ja.house_img.imgbig.id) {
                            img.isCover = 1;
                        }
                        else {
                            img.isCover = 0;
                        }
                    });
                }
            },
            remark_img() {
                this.add_img_button_div = !this.add_img_button_div;
                if (this.add_img_button_div) {
                    const bigImg = this.house_img.imgbig;
                    forEach(ja.house_img.imgsmall, img => {
                        if (img.id === bigImg.id) {
                            img.txt = bigImg.txt;
                        }
                    });
                }
            },

            if_show_mask() {
                this.mask_img_rule = !this.mask_img_rule;
            },

            click_img(img) {
                const bigImg = this.house_img.imgbig;
                bigImg.src = img.src;
                bigImg.id = img.id;
                bigImg.rotate = img.rotate;
                bigImg.order = img.order;
                bigImg.txt = img.txt;
                this.rotate = bigImg.rotate;
                this.img_rotate.transform = 'rotate(' + this.rotate + 'deg)';
            },
            async sen_img_info() {
                const close = fxj.dialog.loading({noBg: true});
                const imgs = ja.house_img.imgsmall.map(img => {
                    delete img.src;
                    img.rotate = (+img.rotate / 90 + 1) % 4;
                    return img;
                });
                const result = await ajax({
                    url: fxj.getSearch()
                        .get('type') === 'add' ? 'home/houseUpload/imgAdd' : 'home/houseManage/houseUpdateImg',
                    data: {
                        imgs,
                        houseId: houseId,
                    },
                });
                close();
                if (result.err === 0) {
                    await toast('提交成功');
                    localStorage.removeItem('hm_picture');
                    if (fxj.getSearch().get('papa') === 'menu') {
                        history.back();
                    }
                    else {
                        goPage('hm_rull.html', {
                            data: {
                                houseId,
                                type: fxj.getSearch().get('type'),
                            },
                        });
                    }
                }
                else {
                    toast(result.msg);
                }
            },
            moveImg_right() {
                const currentImgOrder = Number(ja.house_img.imgbig.order);
                if (currentImgOrder < this.img_all) {
                    const nextImgOrder = currentImgOrder + 1;

                    forEach(this.house_img.imgsmall, img => {
                        if (img.order === currentImgOrder) {
                            img.order = nextImgOrder;
                            this.house_img.imgbig.order = nextImgOrder;
                        }
                        else if (img.order === nextImgOrder) {
                            img.order = currentImgOrder;
                        }
                    });
                }
            },
            moveImg_left() {
                const currentImgOrder = +ja.house_img.imgbig.order;
                if (currentImgOrder > 1) {
                    const prevImgOrder = currentImgOrder - 1;

                    forEach(this.house_img.imgsmall, img => {
                        if (img.order === currentImgOrder) {
                            img.order = prevImgOrder;
                            this.house_img.imgbig.order = prevImgOrder;
                        }
                        else if (img.order === prevImgOrder) {
                            img.order = currentImgOrder;
                        }
                    });
                }
            },
        },
        async created() {
            if (fxj.getSearch().get('type') === 'add') return;
            const close = fxj.dialog.loading({noBg: true});
            const re = await ajax({
                url: 'home/houseDetail/getHouseDetail',
                data: {
                    houseId,
                },
            });
            close();
            if (re.err !== 0) {
                toast(re.msg);
                return;
            }
            this.img_all = re.data.imgs.length;
            const tempImgs = [];
            forEach(re.data.imgs, (img, i) => {
                const tempImg = {};
                tempImg.src = fxj.fixImgUrl(img.surl);
                tempImg.txt = img.notice;
                tempImg.rotate = ((+img.rotate - 1) % 4) * 90;
                tempImg.order = i + 1;
                tempImg.id = img.hi_id;

                if (tempImg.src.includes(re.data.img)) {
                    tempImg.isCover = 1;

                    ja.house_img.imgbig = {
                        id: tempImg.id,
                        src: tempImg.src,
                        txt: tempImg.txt,
                        rotate: tempImg.rotate,
                        order: tempImg.order,
                    };
                    this.rotate = this.house_img.imgbig.rotate;
                    this.img_rotate.transform = `rotate(${this.rotate}deg)`;
                }
                else {
                    tempImg.isCover = 0;
                }
                tempImgs.push(tempImg);
            });
            this.house_img.imgsmall = tempImgs;
        },
    });

    const fileInput = document.getElementById('addimgca');
    fileInput.addEventListener('change', async function (evt) {
        const files = evt.target.files;
        if (files.length === 0) return;
        const close = fxj.dialog.loading({noBg: true, des: '上传图片中'});
        const file = files[0];
        const reader = new FileReader();
        reader.addEventListener('load', async function (result) {
            const imgContainer = new Image();
            imgContainer.src = reader.result;
            imgContainer.onload = async () => {
                const scale = imgContainer.width / imgContainer.height;
                if (scale < 1.3) {
                    close();
                    fxj.dialog.alert({
                        title: '手机请横屏拍摄照片',
                        msg: `照片宽高比例为3:2最佳`,
                        btn: 1,
                        btnLeft: '我知道啦',
                    });
                }
                const formData = new FormData();
                formData.append('token', localStorage.getItem('token'));
                formData.append('houseId', houseId);
                formData.append('img', file);
                let res = await fetch('http://fxj178.com/home/AppUploadImg/imgUpload', {
                    method: 'POST',
                    body: formData,
                });
                close();
                try {
                    res = await res.json();
                } catch (e) {
                    console.error(e);
                    toast('json解析错误');
                    return;
                }
                if (res.err !== 0) {
                    toast(res.msg);
                    return;
                }
                ja.img_all += 1;
                ja.house_img.imgbig = {
                    rotate: 0,
                    txt: '',
                    src: reader.result,
                    name: file.name,
                    order: ja.img_all,
                    id: res.data,
                };

                ja.house_img.imgsmall.push({
                    rotate: ja.house_img.imgbig.rotate,
                    txt: ja.house_img.imgbig.txt,
                    id: ja.house_img.imgbig.id,
                    order: ja.house_img.imgbig.order,
                    src: ja.house_img.imgbig.src,
                    isCover: 0,
                });
            };
        });
        reader.readAsDataURL(file);
    }, false);
}

function _back() {
    if (fxj.getSearch().get('type') === 'add') {
        closeToWin();
    }
    else {
        localStorage.removeItem('hm_picture');
        history.back();
    }
}
